import {createRouter,createWebHashHistory} from 'vue-router'
import { RouteRecordRaw } from "vue-router";
import LocalCache from '../utils/cache'
import {firstMenu} from '../utils/map-menus'
const routes:RouteRecordRaw[] = [
  {
    path:'/',
    redirect:'/main'
  },
  {
    path:'/login',
    component:()=> import('../views/login/login.vue')    
  },
  {
    name:'main',
    path:'/main',
    component:()=> import('../views/main/main.vue'),
    children:[]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: () => import('../views/notfound/notfound.vue')
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory()
})
router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = LocalCache.getCache('token')
    if (!token) {
      return '/login'
    }
  }
  if (to.path === '/main') {
    return firstMenu.url
  }
})
export default router